package com.kjc.demo.basic.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class XMLUtil {
	
	
	
	/**
	 * @param obj 传入的对象
	 * @return 返回对象转换xml字符串
	 */
	public static String ObjToXmlString(Object obj){
		System.out.println("开始将对象转换为XML格式字符串");
		String XmlString = "";
		//创建输出 流
		StringWriter writer = new StringWriter();
		try {
			//jdk自带的转换类
			JAXBContext context = JAXBContext.newInstance(obj.getClass());//传入对象的class
			
			//创建marshaller（指挥）通过它可以将xml与对象互相转换 。对于一些不规范的xml格式它也可以进行规范调试
			Marshaller marshaller = context.createMarshaller();
			
			//格式化xml输出格式
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
			//将对象输出成XML形式
			marshaller.marshal(obj, writer);
			
			XmlString = writer.toString();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			System.out.println("转换出错!!!");
			e.printStackTrace();
		}
		return XmlString;
	}
	
	
	
	/**
	 * @param obj 需要转换xml格式的对象
	 * @param puth 写入xml的路径
	 * @return 
	 */
	public static void ObjToXml(Object obj,String puth){
		
		if(puth!=""){
			System.out.println("开始将对象转换为xml文件");
			try {
				//创建文件输出对象
				FileWriter fileWriter = new FileWriter(new File(puth));
				
				JAXBContext context = JAXBContext.newInstance(obj.getClass());
				
				//创建marshaller
				Marshaller marshaller = context.createMarshaller();
				
				//格式化xml输出格式
				marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
				
				//防止中文乱码 
				marshaller.setProperty(Marshaller.JAXB_ENCODING, "GBK");
				
				//完成输出自动写入
				marshaller.marshal(obj, fileWriter);
				
				System.out.println("已经将对象转换为xml并写入到了指定路径");
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}catch (JAXBException e) {
				// TODO: handle exception
			}
		}else{
			System.out.println("路径为空");
		}
	}
	
	
	
	/**
	 * @param xmlPuth xml文件路径
	 * @param obj 操作的对象
	 * @return xml转换后的对象
	 */
	public static Object XmlToObj(String xmlPuth,Object obj){
		System.out.println("xml读入对象开始");
		Object Oj = new Object();
		try {
			JAXBContext context = JAXBContext.newInstance(obj.getClass());
			
			//创建Unmarshaller
			Unmarshaller unmarshaller = context.createUnmarshaller();
			Oj = unmarshaller.unmarshal(new File(xmlPuth));
			
		}  catch (JAXBException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return Oj;
	}
	
	
	/**
     *  解析xml报文，转化为对象
     * @param string xml报文
     * @return 对象
     */
    public static Object xmlToBean(String xmlString) {
    	Object obj = null;
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(Object.class);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            obj= unmarshaller.unmarshal(new StringReader(xmlString));
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return obj;
    }
    
    /**
     * xml字符串转换成具体的对象
     * */
    public static Object convertXMLToObject(String xml, Object object) {
        Class<?> aClass = object.getClass();
        Object xmlObject = null;
        try {
            JAXBContext context = JAXBContext.newInstance(aClass);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            StringReader sr = new StringReader(xml);
            xmlObject = unmarshaller.unmarshal(sr);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return xmlObject;
    }
}
